using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using SJBotGame.SJBotGameComponents;
using SJBotGame.SJBotGameComponentsDraw;
using SJBotGame;

namespace SJBotGameAnimate
{
    public partial class SJBotGameAnimate_Form : Form
    {
        SJBotTitle botTitle1;
        SJBotTitle botTitle2;
        SJBotTitleDraw botTitleDraw1;
        SJBotTitleDraw botTitleDraw2;
        SJBotTitleHighlightDraw botTitleHighlightDraw;
        SJBotTitleMoveDraw botTitleMoveDraw;
        //
        System.Timers.Timer titleHighlightTimer;
        //
        public SJBotGameAnimate_Form()
        {
            InitializeComponent();
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer, true);
            //
            botTitle1 = new SJBotTitle(1);
            botTitle2 = new SJBotTitle(2);

            botTitleDraw1 = new SJBotTitleDraw(botTitle1, new Rectangle(10, 10, 20, 20));
            botTitleDraw2 = new SJBotTitleDraw(botTitle2, new Rectangle(32, 10, 20, 20));
            //
            botTitleHighlightDraw = new SJBotTitleHighlightDraw();
            botTitleMoveDraw = new SJBotTitleMoveDraw();
            //

            titleHighlightTimer = new System.Timers.Timer();
            titleHighlightTimer.Elapsed += new System.Timers.ElapsedEventHandler(OnTitleHighlightTimer);
            titleHighlightTimer.Interval = 100;
            titleHighlightTimer.Enabled = !titleHighlightTimer.Enabled;
        }

        private void moveTitle_button_Click(object sender, EventArgs e)
        {
            if (botTitleHighlightDraw.TitleDraw != null)
            {
                if (botTitleHighlightDraw.TitleDraw == botTitleDraw1)
                {
                    botTitleMoveDraw.StartMove(botTitleDraw1, botTitleDraw2);
                }
                else if (botTitleHighlightDraw.TitleDraw == botTitleDraw2)
                {
                    botTitleMoveDraw.StartMove(botTitleDraw2, botTitleDraw1);
                }
/*                System.Threading.Thread.Sleep(500);
                //botTitleDraw1.Title = botTitle1;
                botTitleDraw1.Highlight = false;
                //botTitleDraw2.Title = botTitle2;
                botTitleDraw2.Highlight = false;
                botTitleHighlightDraw.StartHighlight(null);*/
            }
        }

        private void changeTitlePos_button_Click(object sender, EventArgs e)
        {
            if (botTitleHighlightDraw.TitleDraw == botTitleDraw1)
            {
                botTitleHighlightDraw.StartHighlight(botTitleDraw2);
                botTitleDraw1.Highlight = false;
                botTitleDraw2.Highlight = true;
            }
            else if (botTitleHighlightDraw.TitleDraw == botTitleDraw2)
            {
                botTitleHighlightDraw.StartHighlight(null);
                botTitleDraw2.Highlight = false;
            }
            else
            {
                botTitleHighlightDraw.StartHighlight(botTitleDraw1);
                botTitleDraw1.Highlight = true;
            }
        }

        private void OnTitleHighlightTimer(object source, System.Timers.ElapsedEventArgs e)
        {
            botTitleHighlightDraw.Update();
            botTitleMoveDraw.Update();
            Redraw();
        }

        private void Redraw()
        {
            botTitleHighlightDraw.Redraw(this.CreateGraphics());
            botTitleMoveDraw.Redraw(this.CreateGraphics());
            //
            if (botTitleHighlightDraw.TitleDraw == botTitleDraw2)
            {
                botTitleDraw1.Draw(this.CreateGraphics());
                botTitleDraw2.Draw(this.CreateGraphics());
            }
            else
            {
                botTitleDraw2.Draw(this.CreateGraphics());
                botTitleDraw1.Draw(this.CreateGraphics());
            }

            //
        }

        private void SJBotGameAnimate_Form_Paint(object sender, PaintEventArgs e)
        {
            Redraw();
        }
    }
}